global datafolder "/.../data"
global outputfolder "/.../output"

capture log close
log using "$outputfolder/log_teams", replace
set more off

** Basic summary stats for draft
use "$datafolder/main_dataset.dta", clear
* number of firms in sample (Section 2.1)
codebook combid if t_1_3==1  // 951
codebook combid if t_1_3==1 & combid<1000000  // 588 
codebook combid if t_1_3==1 & combid>1000000  // 363
unique firmbankt if outcome_indep==1 & year>=1980 & year<=2015 & chapterPrior ==0
unique firmbankt if outcome_fbuy==1 & year>=1980 & year<=2015 & chapterPrior ==0 
unique firmbankt if outcome_obuy==1 & year>=1980 & year<=2015 & chapterPrior ==0 
unique firmbankt if outcome_liq==1 & year>=1980 & year<=2015 & chapterPrior ==0 

** Figure 1
use "$datafolder/main_dataset.dta", clear
keep if t_1_3==1
keep combid yearbankt
duplicates drop 
graph bar (count) combid if year<=2015, over(yearbankt, label(labsize(small)) ///
    relabel(1 "1982" 2 " " 3 " " 4 "1985"  5 " " 6 " " 7 " " 8 " " 9 "1990" 10 " " 11 " " 12 " " 13 " " 14 "1995"  ///
    15 " " 16 " " 17 " " 18 " " 19 "2000"  20 " " 21 " " 22 " " 23 " " 24 "2005" 25 " " 26 " " 27 " " 28 " " 29 "2010" ///
    30 " " 31 " " 32 " " 33 " " 34 "2015"  )) ///
    graphregion(color(white)) ytitle("Frequency")
graph export "$outputfolder/fig1.png", replace	

** Figure 2
use "$datafolder/main_dataset.dta", clear
keep if t_1_3==1
keep combid yearbankt bfirmmodalcpc
duplicates drop 
g cpc =1 if bfirmmodalcpc=="A"
replace cpc =2 if bfirmmodalcpc=="B"
replace cpc =3 if bfirmmodalcpc=="C"
replace cpc =4 if bfirmmodalcpc=="D"
replace cpc =5 if bfirmmodalcpc=="E"
replace cpc =6 if bfirmmodalcpc=="F"
replace cpc =7 if bfirmmodalcpc=="G"
replace cpc =8 if bfirmmodalcpc=="H"
label define cpcvalue 1 "Human necessities" 2 "Performing operations; transporting" 3 "Chemistry; metallurgy" 4 "Textiles; paper" 5 "Fixed constructions" 6 "Mechanical engineering; lighting; heating; weapons; blasting engines or pumps" 7 "Physics" 8 "Electricity"
label values cpc cpcvalue
label variable combid "firms"
graph bar (count) combid if year<=2015, over(cpc, lab(angle(60))) graphregion(color(white)) ytitle("Frequency")
graph export "$outputfolder/fig2.png", replace	

** Figure 3
use "$datafolder/fig3_data.dta", clear 
line coauthor year if year>=1975 & year<=2020, graphregion(color(white)) xlab(1975(10)2020) ylab(1(.5)2.7)
graph export "$outputfolder/fig3.png", replace

** Figure 4
use "$datafolder/fig4_data.dta", clear
set more off
foreach v of varlist roa lev1 logta invone {
eststo 	`v':  quietly reghdfe `v'  ///
		i.t_4 i.t_3 i.t_2 i.t_1 i.t0 ///
		 if year>=1980 & samplet_5 ~=., ///
		absorb(combid yrInd4) vce(cl combid)	
}
coefplot roa, drop(_cons) ///
	order(i.t_4 i.t_3 i.t_2 i.t_1 i.t0)   ///
    vertical graphregion(color(white)) ///
	 /// xtitle("Years since bankruptcy") 
    xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0", tlcolor(gs8)) ///
	ylab(, tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
	subtitle(Panel A. {it:ROA},size(small)) ///
	name(roa, replace)
coefplot lev1, drop(_cons) ///
	order(i.t_4 i.t_3 i.t_2 i.t_1 i.t0)   ///
    vertical graphregion(color(white)) ///
	 /// xtitle("Years since bankruptcy") 
    xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0", tlcolor(gs8)) ///
	ylab(, tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
	subtitle(Panel B. {it:Leverage},size(small)) ///
	name(lev1, replace)
coefplot logta, drop(_cons) ///
	order(i.t_4 i.t_3 i.t_2 i.t_1 i.t0)   ///
    vertical graphregion(color(white)) ///
	 /// xtitle("Years since bankruptcy") 
    xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0", tlcolor(gs8)) ///
	ylab(, tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
	subtitle(Panel C. {it:Ln(Total Assets)},size(small)) ///
	name(logta, replace)
coefplot invone, drop(_cons) ///
	order(i.t_4 i.t_3 i.t_2 i.t_1 i.t0)   ///
    vertical graphregion(color(white)) ///
	 /// xtitle("Years since bankruptcy") 
    xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0", tlcolor(gs8)) ///
	ylab(, tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
	subtitle(Panel D. {it:Number of inventors},size(small)) ///
	name(invone, replace)
gr combine roa lev1 logta invone, ///
	r(2) ysize(4) xsize(4) graphregion(color(white)) b1title("Years since bankruptcy",size(vsmall)) //ysize(3) xsize(4) 
graph export "$outputfolder/fig4.png", replace 	

** Figure 5
use "$datafolder/main_dataset.dta", clear
* t-1 as reference
set more off
foreach v of varlist LNconeInvNewo LNponeInvNewo LNcpInvNewo LNxi_nominal{ //
foreach w in tb {  //tt
eststo `v'_10`w':  reghdfe `v'  ///
		 `w'10 `w'9 `w'8 `w'7 `w'6 `w'5 `w'4 `w'3 `w'2 `w'1 `w'0 `w'_2 `w'_3 `w'_4 `w'_5 `w'_6 `w'_7 `w'_8 `w'_9 `w'_10  ///
		 i.t10 i.t9 i.t8 i.t7 i.t6 i.t5 i.t4 i.t3 i.t2 i.t1 i.t0 i.t_2 i.t_3 i.t_4 i.t_5 i.t_6 i.t_7 i.t_8 i.t_9 i.t_10  ///
		if year>=1980 & year<=2015 & t1010==1 , ///
		absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt)
}
}
* four graphs
coefplot LNconeInvNewo_10tb, drop(_cons  ///
	1.t10 1.t9 1.t8 1.t7 1.t6 1.t5 1.t4 1.t3 1.t2 1.t1 1.t0  1.t_2 1.t_3 1.t_4 1.t_5 1.t_6 1.t_7 1.t_8 1.t_9) ///
	order(tb_10 tb_9 tb_8 tb_7 tb_6 tb_5 tb_4 tb_3 tb_2 1.t_10 tb0 tb1 tb2 tb3 tb4 tb5 tb6 tb7 tb8 tb9 tb10) ///
    vertical graphregion(color(white)) ///
    xlab(1 "-10" 3 "-8" 5 "-6" 7 "-4" 9 "-2" 11 "0" 13 "2" 15 "4" 17 "6" 19 "8" 21 "10", tlcolor(gs8)) ///
	ylab(-.6(0.2).6,tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
    xsize(1) ysize(1) name(LNconeInvNewo_10tb, replace) subtitle(Panel C. {it:Ln(Citations)},size(small))
	//    yline(0, lw(thin) lcolor(black)) xline(10, lw(thin) lpattern(longdash))  /// added lines
coefplot LNponeInvNewo_10tb, drop(_cons  ///
	1.t10 1.t9 1.t8 1.t7 1.t6 1.t5 1.t4 1.t3 1.t2 1.t1 1.t0  1.t_2 1.t_3 1.t_4 1.t_5 1.t_6 1.t_7 1.t_8 1.t_9) ///
	order(tb_10 tb_9 tb_8 tb_7 tb_6 tb_5 tb_4 tb_3 tb_2 1.t_10 tb0 tb1 tb2 tb3 tb4 tb5 tb6 tb7 tb8 tb9 tb10) ///
    vertical graphregion(color(white)) ///
    xlab(1 "-10" 3 "-8" 5 "-6" 7 "-4" 9 "-2" 11 "0" 13 "2" 15 "4" 17 "6" 19 "8" 21 "10", tlcolor(gs8)) ///
	ylab(-.3(0.1).3,tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
    xsize(1) ysize(1) name(LNponeInvNewo_10tb, replace) subtitle(Panel A. {it:Ln(Patents)},size(small))
	//    yline(0, lw(thin) lcolor(black)) xline(10, lw(thin) lpattern(longdash))  /// added lines
coefplot LNcpInvNewo_10tb, drop(_cons  ///
	1.t10 1.t9 1.t8 1.t7 1.t6 1.t5 1.t4 1.t3 1.t2 1.t1 1.t0  1.t_2 1.t_3 1.t_4 1.t_5 1.t_6 1.t_7 1.t_8 1.t_9) ///
	order(tb_10 tb_9 tb_8 tb_7 tb_6 tb_5 tb_4 tb_3 tb_2 1.t_10 tb0 tb1 tb2 tb3 tb4 tb5 tb6 tb7 tb8 tb9 tb10) ///
    vertical graphregion(color(white)) ///
    xlab(1 "-10" 3 "-8" 5 "-6" 7 "-4" 9 "-2" 11 "0" 13 "2" 15 "4" 17 "6" 19 "8" 21 "10", tlcolor(gs8)) ///
	ylab(-.4(0.1).4,tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
    xsize(1) ysize(1) name(LNcpInvNewo_10tb, replace) subtitle(Panel B. {it:Ln(Citations per patent)},size(small))
	//    yline(0, lw(thin) lcolor(black)) xline(10, lw(thin) lpattern(longdash))  /// added lines
coefplot LNxi_nominal_10tb, drop(_cons  ///
	1.t10 1.t9 1.t8 1.t7 1.t6 1.t5 1.t4 1.t3 1.t2 1.t1 1.t0  1.t_2 1.t_3 1.t_4 1.t_5 1.t_6 1.t_7 1.t_8 1.t_9) ///
	order(tb_10 tb_9 tb_8 tb_7 tb_6 tb_5 tb_4 tb_3 tb_2 1.t_10 tb0 tb1 tb2 tb3 tb4 tb5 tb6 tb7 tb8 tb9 tb10) ///
    vertical graphregion(color(white)) ///
    xlab(1 "-10" 3 "-8" 5 "-6" 7 "-4" 9 "-2" 11 "0" 13 "2" 15 "4" 17 "6" 19 "8" 21 "10", tlcolor(gs8)) ///
	ylab(-.6(0.2).6,tlcolor(gs8))  /// tick
    ciopts(recast(rcap) lpattern(longdash)) ///  ci
    msymbol(o) mlwidth(medthick) mfcolor(gs16)  ///  marker
    yscale(lwidth(medthick)) xscale(lwidth(medthick))  /// asix lwidth
	yline(0, lw(thin) lcolor(black))   /// line y=0
    ysc(noextend) xsc(noextend)   /// disconnected axes
    omitted  /// omit requests to plot omitted coefficients
    xsize(1) ysize(1) name(LNxi_nominal_10tb, replace) subtitle(Panel D. {it:Ln(Dollar value of patents)},size(small))
	//    yline(0, lw(thin) lcolor(black)) xline(10, lw(thin) lpattern(longdash))  /// added lines
gr combine LNponeInvNewo_10tb LNcpInvNewo_10tb LNconeInvNewo_10tb LNxi_nominal_10tb, r(2) ysize(4) xsize(4) ///
    b1title("Years since bankruptcy",size(vsmall)) graphregion(color(white))
graph export "$outputfolder/fig5.png", replace	


** Table 1: Summary statistics, Panel A (summary stats for Panels B and C are below)
* spec 1 from Table 3
reghdfe LNponeInvNewo Post if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
labsumm LNponeInvNewo LNcpInvNewo LNconeInvNewo LNxi_nominal Post co_bt1 t0 t1 t2 t3 t4 t5plus t3plus if e(sample) 
labsumm nca nca_dummy outcome_indep outcome_fbuy outcome_obuy outcome_liq BanktTenure tb_t4_dum if e(sample) 
labsumm cpcChgT3 general_befbankt actYrRoll if e(sample) 

** Table 2: treated vs nontreated
use "$datafolder/table2_data.dta", clear
* Panel A
tab cpc TreatedD if ((Post==0 & TreatedD==1 & year < yearbankt) | (TreatedD==0 & experience>1)) & cpc1~="Y" & year>=1980 & year<=2015
* Panel B
tabstat poneInvNewo coneInvNewo cpInvNewo ///
	if ((Post==0 & TreatedD==1 & year < yearbankt) | (TreatedD==0 & experience>1)) & year>=1980 & year<=2015, ///
	s(N mean sd ) f(%12.3f) columns(statistics) lo by(TreatedD)
tabstat poneInvLife coneInvLife quinPone quinCone experience numInvPPat ///
	if ((Post==0 & TreatedD==1 & year < yearbankt) | (TreatedD==0 & experience>1)) & inventorlevel ==1 & year>=1980 & year<=2015, /// 
	s(N mean sd ) f(%12.3f) columns(statistics) lo by(TreatedD)
	
** Table 3: individual inventors
use "$datafolder/main_dataset.dta", clear
* Panel A
eststo table3_PA_1: reghdfe LNponeInvNewo Post if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table3_PA_2: reghdfe LNcpInvNewo Post if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table3_PA_3: reghdfe LNponeInvNewo t0 t1 t2 t3 t4 t5plus if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table3_PA_4: reghdfe LNcpInvNewo t0 t1 t2 t3 t4 t5plus if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
esttab table3_PA_1 table3_PA_2 table3_PA_3 table3_PA_4 ///
	using "$outputfolder/T3_PA.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	keep(Post t0 t1 t2 t3 t4 t5plus)  ///
	order(Post t0 t1 t2 t3 t4 t5plus) ///
	nogaps nolegend nonotes mlabel()
drop _est*
* Panel B
eststo table3_PB_1: reghdfe LNponeInvNewo i.Post##c.cpcChgT3 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table3_PB_2: reghdfe LNcpInvNewo i.Post##c.cpcChgT3 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt)
eststo table3_PB_3: reghdfe LNponeInvNewo i.Post##c.general_befbankt if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table3_PB_4: reghdfe LNcpInvNewo i.Post##c.general_befbankt if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
esttab table3_PB_1 table3_PB_2 table3_PB_3 table3_PB_4 ///
	using "$outputfolder/T3_PB.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

** Table 4: stay together
eststo table4_1: reghdfe tb_t4_dum i.t0 i.t1 i.t2 i.t3plus if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt)  
eststo table4_2: reghdfe tb_t4_dum i.t0##i.outcome_liq i.t1##i.outcome_liq i.t2##i.outcome_liq i.t3plus##i.outcome_liq if chapterPrior==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table4_3: reghdfe tb_t4_dum i.t0##i.outcome_indep i.t1##i.outcome_indep i.t2##i.outcome_indep i.t3plus##i.outcome_indep if chapterPrior==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table4_4: reghdfe tb_t4_dum i.t0##i.outcome_fbuy i.t1##i.outcome_fbuy i.t2##i.outcome_fbuy i.t3plus##i.outcome_fbuy if chapterPrior==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table4_5: reghdfe tb_t4_dum i.t0##i.outcome_obuy i.t1##i.outcome_obuy i.t2##i.outcome_obuy i.t3plus##i.outcome_obuy if chapterPrior==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
eststo table4_6: reghdfe tb_t4_dum i.t0##i.nca_dummy i.t1##i.nca_dummy i.t2##i.nca_dummy i.t3plus##i.nca_dummy if chapterPrior==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear actYrRoll) vce(cl firmbankt) 
sum tb_t4_dum if e(sample) & Post ==0
esttab table4_1 table4_2 table4_3 table4_4 table4_5 table4_6 ///
	using "$outputfolder/T4.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*
 
** Table 5: teams & bankruptcy
* Panel A
eststo table5_PA_1: reghdfe LNponeInvNewo Post co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(firmbankt techyear actYrRoll) vce(cl firmbankt) 
eststo table5_PA_2: reghdfe LNcpInvNewo Post co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(firmbankt techyear actYrRoll) vce(cl firmbankt) 
eststo table5_PA_3: reghdfe LNponeInvNewo Post co_bt1 P_cobt1 if year>=1980 & year<=2015 & t1010==1, absorb(firmbankt techyear actYrRoll) vce(cl firmbankt) 
eststo table5_PA_4: reghdfe LNcpInvNewo Post co_bt1 P_cobt1 if year>=1980 & year<=2015 & t1010==1, absorb(firmbankt techyear actYrRoll) vce(cl firmbankt) 
esttab table5_PA_1 table5_PA_2 table5_PA_3 table5_PA_4 ///
	using "$outputfolder/T5_PA.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	keep(Post co_bt1 P_cobt1)  ///
	order(Post co_bt1 P_cobt1) ///
	nogaps nolegend nonotes mlabel()
drop _est*
* Panel B
eststo table5_PB_1: reghdfe LNponeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table5_PB_2: reghdfe LNcpInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table5_PB_3: reghdfe LNponeInvNewo i.t0##c.co_bt1 i.t1##c.co_bt1 i.t2##c.co_bt1 i.t3##c.co_bt1 i.t4##c.co_bt1 i.t5plus##c.co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table5_PB_4: reghdfe LNcpInvNewo i.t0##c.co_bt1 i.t1##c.co_bt1 i.t2##c.co_bt1 i.t3##c.co_bt1 i.t4##c.co_bt1 i.t5plus##c.co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
esttab table5_PB_1 table5_PB_2 table5_PB_3 table5_PB_4 ///
	using "$outputfolder/T5_PB.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

** Table 6: robustness
* spec 1: star inventors
eststo table6_1: reghdfe LNponeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1 & quinPone>90, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* spec 2: below median R&D
preserve
reghdfe LNponeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
keep if e(sample) 
keep if t_3==1
keep combid rdat
duplicates drop
sum rdat, d
restore
g rdat0temp =1 if rdat<.049815 & t_3==1
bysort inventor (year): egen rdat0 = total(rdat0temp)
drop rdat0temp
eststo table6_2: reghdfe LNponeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1 & rdat0==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* spec 3: ln(citations)
eststo table6_3: reghdfe LNconeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* spec 4:ln(dollar value)
eststo table6_4: reghdfe LNxi_nominal Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* spec 5: control for tenure
eststo table6_5: reghdfe LNponeInvNewo Post P_cobt1 co_bt1 c.Post##c.BanktTenure if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* spec 6: include all nontreated inventors
keep inventor year Post co_bt1 P_cobt1 t1010 firmbankt
duplicates drop
so inventor year
tempfile temp
save `temp'
use "$datafolder/table2_data.dta", clear // dataset from Table 2 - all treated and nontreated
so inventor year
merge 1:1 inventor year using `temp'
drop _m
egen techyear = group(cpc1 year)
bysort inventor (year): egen startYr = min(year)
g actYrRoll = year-startYr+1 if startYr~=.
sum actYrRoll
replace Post = 0 if Post ==.
replace P_cobt1 = 0 if P_cobt1 ==.
replace co_bt1 = 0 if co_bt1 ==.
replace t1010=0 if t1010==.
replace firmbankt = 0 if firmbankt==.
gen LNponeInvNewo = ln(1+poneInvNewo)
eststo table6_6: reghdfe LNponeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & ((TreatedD==1 & t1010==1) | (TreatedD==0 & experience>1)), absorb(inventor techyear firmbankt#year actYrRoll) vce(cl firmbankt) 
* spec 7: treated = at bankrupt firm one year prior to bankruptcy
use "$datafolder/main_dataset.dta", clear
so inventor
merge m:1 inventor using "$datafolder/indata_t_1"
keep if _m==3
drop _m
eststo table6_7: reghdfe LNponeInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* Poisson model
use "$datafolder/main_dataset.dta", clear
eststo table6_8: ppmlhdfe poneInvNewo Post P_cobt1 co_bt1 if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
esttab table6_1 table6_2 table6_3 table6_4 table6_5 table6_6 table6_7 table6_8 ///
	using "$outputfolder/T6.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

** Table 7: stop inventing
use "$datafolder/table7_data.dta", clear
eststo table7_1: reghdfe live1 co_bt1 if min~=max & year>=1980 & year<=2015 & inventorlevel ==1, vce(cl firmbankt) a(firmbankt cpc experience)
labsumm live1-live5 co_bt1 if e(sample) 
eststo table7_2: reghdfe live2 co_bt1 if min~=max & year>=1980 & year<=2015 & inventorlevel ==1, vce(cl firmbankt) a(firmbankt cpc experience)
eststo table7_3: reghdfe live3 co_bt1 if min~=max & year>=1980 & year<=2015 & inventorlevel ==1, vce(cl firmbankt) a(firmbankt cpc experience)
eststo table7_4: reghdfe live4 co_bt1 if min~=max & year>=1980 & year<=2015 & inventorlevel ==1, vce(cl firmbankt) a(firmbankt cpc experience)
eststo table7_5: reghdfe live5 co_bt1 if min~=max & year>=1980 & year<=2015 & inventorlevel ==1, vce(cl firmbankt) a(firmbankt cpc experience)
esttab table7_1 table7_2 table7_3 table7_4 table7_5 ///
	using "$outputfolder/T7.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

** Table 8: team productivity, bankruptcy outcomes
use "$datafolder/main_dataset.dta", clear
* outcome liq
eststo table8_1: reghdfe LNponeInvNewo i.Post##i.outcome_liq c.P_cobt1##i.outcome_liq if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table8_2: reghdfe LNcpInvNewo i.Post##i.outcome_liq c.P_cobt1##i.outcome_liq if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* outcome indep
eststo table8_3: reghdfe LNponeInvNewo i.Post##i.outcome_indep c.P_cobt1##i.outcome_indep if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table8_4: reghdfe LNcpInvNewo i.Post##i.outcome_indep c.P_cobt1##i.outcome_indep if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* outcome fbuy
eststo table8_5: reghdfe LNponeInvNewo i.Post##i.outcome_fbuy c.P_cobt1##i.outcome_fbuy if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table8_6: reghdfe LNcpInvNewo i.Post##i.outcome_fbuy c.P_cobt1##i.outcome_fbuy if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* outcome other buy
eststo table8_7: reghdfe LNponeInvNewo i.Post##i.outcome_obuy c.P_cobt1##i.outcome_obuy if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table8_8: reghdfe LNcpInvNewo i.Post##i.outcome_obuy c.P_cobt1##i.outcome_obuy if chapterPrior ==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
esttab table8_1 table8_2 table8_3 table8_4 table8_5 table8_6 table8_7 table8_8 ///
	using "$outputfolder/T8.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

** Table 9: noncompete agreements
* nca_dummy==0 (NCA < 5)
eststo table9_1: reghdfe LNponeInvNewo Post P_cobt1 if nca_dummy==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table9_2: reghdfe LNcpInvNewo Post P_cobt1 if nca_dummy==0 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* nca_dummy == 1 (NCA > 4)
eststo table9_3: reghdfe LNponeInvNewo Post P_cobt1 if nca_dummy==1 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table9_4: reghdfe LNcpInvNewo Post P_cobt1 if nca_dummy==1 & year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
* interact with nca_dummy
eststo table9_5: reghdfe LNponeInvNewo i.Post##c.nca_dummy c.P_cobt1##c.nca_dummy if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
eststo table9_6: reghdfe LNcpInvNewo i.Post##c.nca_dummy c.P_cobt1##c.nca_dummy if year>=1980 & year<=2015 & t1010==1, absorb(inventor techyear firmbanktyear actYrRoll) vce(cl firmbankt) 
esttab table9_1 table9_2 table9_3 table9_4 table9_5 table9_6 ///
	using "$outputfolder/T9.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

** Table 10: teams and joint mobility
use "$datafolder/table10_data.dta", clear
* summary stats for Table 1, Panel C
eststo table10_1: reg moveTgt paircoT1,  vce(cl firmbankt)
labsumm moveTgt paircoT1 citmean nca_dummy outcome_obuy outcome_liq if e(sample)
* regressions for Table 10
eststo table10_2: reg moveTgt paircoT1 nca_dummy,  vce(cl firmbankt)
eststo table10_3: reg moveTgt paircoT1 nca_dummy outcome_obuy,  vce(cl firmbankt)
eststo table10_4: reg moveTgt paircoT1 nca_dummy outcome_obuy outcome_liq,  vce(cl firmbankt)
eststo table10_5: reg moveTgt citmean,  vce(cl firmbankt)
eststo table10_6: reg moveTgt citmean nca_dummy,  vce(cl firmbankt)
eststo table10_7: reg moveTgt citmean nca_dummy outcome_obuy,  vce(cl firmbankt)
eststo table10_8: reg moveTgt citmean nca_dummy outcome_obuy outcome_liq,  vce(cl firmbankt)
esttab table10_1 table10_2 table10_3 table10_4 table10_5 table10_6 table10_7 table10_8 ///
	using "$outputfolder/T10.csv", ///
	replace label b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(r2_a N, fmt(%3s %3s %3s %3s 3 %20.0fc)) ///
	nogaps nolegend nonotes mlabel()
drop _est*

log close
